package day10.test1;

class ObjectValueService {
    private String lock = "123";
    private int sharedResource = 0;

    public void testMethod() {
        try {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() +
                        "  begin " + System.currentTimeMillis());
                //改变lock对象的值
                lock = "456";
                sharedResource++; // 操作共享资源
                System.out.println(Thread.currentThread().getName() + " sharedResource value: " + sharedResource);
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName() +
                        "  end   " + System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ObjectValueThread extends Thread {
    private ObjectValueService service;

    public ObjectValueThread(ObjectValueService s) {
        super();
        this.service = s;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

public class RunObjectValue {
    public static void main(String[] args) throws InterruptedException {
        ObjectValueService object = new ObjectValueService();
        ObjectValueThread a = new ObjectValueThread(object);
        a.setName("A");
        ObjectValueThread b = new ObjectValueThread(object);
        b.setName("B");
        a.start();
        //Thread.sleep(50);
        b.start();
    }
}